import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.*;


/**
 * responsible  to get a file from the server
 * 
 * @author Vladimir Sarkisyan & Erez Levy
 *
 */
public class FileServerForFilesFromTheServer implements Runnable{

	/**
	 * the function get a file from the server
	 * 
	 * the function open ServerSocket in port 4445
	 * and listen for the server that might transfer a file
	 */
	@Override
	public void run() {
		// TODO Auto-generated method stub
        Thread t1=new Thread(new FileReciever());
        t1.run();
	}
	
	private class FileReciever implements Runnable
    {
        public void run()
        {
                    ServerSocket servsock;
                        try 
                        {
                                servsock = new ServerSocket(4445);
                            while (true) 
                            {
                              System.out.println("Waiting...");
                              Socket sock = servsock.accept();
                              System.out.println("Accepted connection : " + sock);
                           // sendfile
                              new Thread(new getFile(sock)).run();
                            }
                        } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
        }
    }
	//end file Reciever

    private class getFile implements Runnable
    {
        private Socket sock;
        public getFile(Socket sock) {
        // TODO Auto-generated constructor stub
                this.sock=sock;
        }
        @Override
                public void run() 
                {
                        // TODO Auto-generated method stub
                        try 
                        {
                                int bytesRead;
                                int current = 0;
                                System.out.println("Connecting...");
                                
                            InputStream in = sock.getInputStream();
        
                            DataInputStream din = new DataInputStream(in);
        
                           //First read the file name
        
                            String filename=din.readUTF();
                                long filesize = din.readLong();
                                System.out.println(filesize);
                                byte [] bytes = new byte [(int) filesize];
                                FileOutputStream fos = new FileOutputStream(Declarations.instance.getPath()+filename);
                                BufferedOutputStream bos = new BufferedOutputStream(fos);
                                bytesRead = in.read(bytes,0,bytes.length);
                                current = bytesRead;
                                do 
                                {
                                   bytesRead = in.read(bytes, current, (bytes.length-current));
                                   if(bytesRead >= 0) 
                                        current = current+bytesRead;
                                } while(bytesRead > 0);
                
                                bos.write(bytes, 0 , current);
                                bos.flush();
                                bos.close();
                                    
   
                        System.out.println("Recieved");
                            sock.close();
                        }
                    catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                          e.printStackTrace();
                
                    }
                    catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }
    }
}
